﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace sqxt
{
    public partial class health_problem : Form
    {
        public health_problem()
        {
            InitializeComponent();
        }
        string hno = "";// 健康档案编号
        SqlDataAdapter sda;
        DataTable dt;
        SqlCommandBuilder scb;
        SqlDataReader sdr;
        SqlConnection conn = new SqlConnection(Dbconnect.s1);
        SqlCommand sc;
        bool xiugai = false;//是否是修改按钮传过来
        int id;//健康问题编号
        public health_problem(string s)
        {
            InitializeComponent();
            hno = s;
        }
        public health_problem(int i, bool xg)
        {
            xiugai = xg;
            id = i;
            InitializeComponent();

        }

        private void health_problem_Load(object sender, EventArgs e)
        {
            try
            {
                if (xiugai)
                {
                    conn.Open();
                    sc = new SqlCommand("select * from person_problem where id="+id.ToString(), conn);
                    sdr = sc.ExecuteReader();
                    sdr.Read();
                    {
                        comboBox2.Text = sdr["problem_type"].ToString();
                        dateTimePicker1.Value =DateTime.Parse(sdr["problem_begintime"].ToString());
                        textBoxms.Text = sdr["problem_describe"].ToString();
                        textBoxjz.Text = sdr["problem_jz"].ToString();
                        richTextBox2.Text = sdr["checkbody"].ToString();
                        richTextBox4.Text = sdr["problem_evaluate"].ToString();
                        richTextBox3.Text = sdr["problen_plan"].ToString();
                        richTextBox1.Text = sdr["direction"].ToString();
                        comboBox1.Text = sdr["is_end"].ToString();
                        dateTimePicker1.Value =DateTime.Parse(sdr["problem_recordtime"].ToString());
                        conn.Close();
                        sdr.Close();
                    }
                }
            }
            catch (Exception e1)
            {
                
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }

            }

        }
      
        private void buttonsave_Click(object sender, EventArgs e)//保存
        {
            try
            {
                if (xiugai)
                {
                    sda = new SqlDataAdapter("select * from person_problem", Dbconnect.s1);
                    dt = new DataTable();
                    sda.Fill(dt);
                    dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
                    DataRow dr1 = dt.Rows.Find(id);//找出要修改信息的哪一行
                    dr1["problem_type"] = comboBox2.Text.Trim();
                    dr1["problem_begintime"] = dateTimePicker1.Value.ToShortTimeString();
                    dr1["problem_describe"] = textBoxms.Text.Trim();
                    dr1["problem_jz"] = textBoxjz.Text.Trim();
                    dr1["checkbody"] = richTextBox2.Text.Trim();
                    dr1["problem_evaluate"] = richTextBox4.Text.Trim();
                    dr1["problen_plan"] = richTextBox3.Text.Trim();
                    dr1["direction"] = richTextBox1.Text.Trim();
                    dr1["is_end"] = comboBox1.Text.Trim();
                    dr1["problem_recordtime"] = dateTimePicker1.Value.ToShortTimeString();
                    scb = new SqlCommandBuilder(sda);

                    sda.Update(new DataRow[] { dr1 });
                    MessageBox.Show("已成功修改");
                  
                }
                else
                {
                    sda = new SqlDataAdapter("select * from person_problem", Dbconnect.s1);
                    dt = new DataTable();
                    sda.Fill(dt);
                    dt.PrimaryKey = new DataColumn[] { dt.Columns["person_healthfile_no"] };
                    DataRow dr;
                    dr = dt.NewRow();
                    dr["person_healthfile_no"] = hno;
                    dr["problem_type"] = comboBox2.Text.Trim();
                    dr["problem_begintime"] = dateTimePicker1.Value.ToShortTimeString();
                    dr["problem_describe"] = textBoxms.Text.Trim();
                    dr["problem_jz"] = textBoxjz.Text.Trim();
                    dr["checkbody"] = richTextBox2.Text.Trim();
                    dr["problem_evaluate"] = richTextBox4.Text.Trim();
                    dr["problen_plan"] = richTextBox3.Text.Trim();
                    dr["direction"] = richTextBox1.Text.Trim();
                    dr["is_end"] = comboBox1.Text.Trim();
                    dr["problem_recordtime"] = dateTimePicker1.Value.ToShortTimeString();
                    dt.Rows.Add(dr);
                    scb = new SqlCommandBuilder(sda);
                    sda.Update(dt);
                    MessageBox.Show("居民健康问题已创建成功");
                }
                    this.Close();
                
            }
            catch (Exception e1)
            {
                

            } 
        }
    }
}